﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;


namespace OrganizeYourSearch
{
    /// <summary>
    /// Interaction logic for WinInsertTag.xaml
    /// </summary>
    public partial class WinInsertTag : Window
    {   
        /// <summary>
        /// The tag word in current context
        /// </summary>
        public Tag CurTag{get;set;}

        /// <summary>
        /// The current instance of the tag word selected, in current context.
        /// Represents the tagged document and the text selected for tagging.
        /// </summary>
        public TagInstanceDetail CurTagInstance{get;set;}


        /// <summary>
        /// We create a tag Instance from the arguments provided by the docuement which creates this
        /// dialog window
        /// </summary>
        public TagInstanceDetail BuildTagInstance{get;set;}


        /// <summary>
        ///  Collection of Tag objects that we use
        /// </summary>
        public ObservableCollection<Tag> Tags { get; set; }

        /// <summary>
        /// Default Constructor
        /// </summary>
        public WinInsertTag()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="selDocTxt">SelectedText in document</param>
        /// <param name="title">Title of the document</param>
        /// <param name="locPath">File Path of the selected document</param>
        /// <param name="startPt">starting index of the text selected for tagging</param>
        /// <param name="endPt">index of the endpoint of the text selected for tagging</param>
        public WinInsertTag(string selDocTxt,string title, string locPath, long startPt, long endPt)
        {
            BuildTagInstance = new TagInstanceDetail(0, 0, startPt, endPt, locPath, title, selDocTxt);
            try
            {
                InitializeComponent();
                Tags = new ObservableCollection<Tag>();
                CurTag = new Tag();
                
                
                Tags = WPFApplication.Database.GetTags(SearchBox.Text);
                LstTags.ItemsSource = Tags;
                LstTags.DisplayMemberPath = "TagName";

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Inserts the text selected in the document as the instance of the currently selected tag word.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LstTags_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CurTag = LstTags.SelectedValue as Tag;
            if (CurTag != null)
            {
                BuildTagInstance.TagId = CurTag.TagId;
                try
                {
                    WPFApplication.Database.InsertIntoTagInstanceDetails(BuildTagInstance);
                    AddCurTagWordToWpInsertedTags();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("From WinInsertTag file, LstTags_Selection Changed" + ex.Message);
                }
                SearchBox.Text = "";
            }
            
        }

        /// <summary>
        /// Add selected tag word to Wrap panel which contains the list of selected tag words 
        /// </summary>
        private void AddCurTagWordToWpInsertedTags()
        {
            try
            {
                TextBlock tblk = new TextBlock();
                tblk.Margin = new Thickness(2, 1, 2, 1);
                tblk.Text = CurTag.TagName;
                wpInsertedTags.Children.Add(tblk);
            }
            catch (Exception ex)
            {
                MessageBox.Show("From AddCurItemToWpInsertedTags() \n" + ex.Message);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDoneTagging_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Event triggered when the Tag Button is clicked. 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTag_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                long tagId=WPFApplication.Database.InsertIntoTags(SearchBox.Text);
                BuildTagInstance.TagId = tagId;
                WPFApplication.Database.InsertIntoTagInstanceDetails(BuildTagInstance);
                CurTag = new Tag()
                            {
                                TagId = tagId,
                                TagName = SearchBox.Text,
                                CurrentFreq = 1,
                                SizeFactor = Helpers.TagHelper.CalculateSizeFactor(1,
                                                                WPFApplication.Database.MaxFreq,
                                                                WPFApplication.Database.MinFreq)
                            };
                AddCurTagWordToWpInsertedTags();
                RefreshTagListInListBox();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        /// <summary>
        /// Reflects the change in textbox to the filteration of items in listbox tag collection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbSearchBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            RefreshTagListInListBox();
        }

        /// <summary>
        /// Recalculates the list of tags to be displayed in the tag list
        /// </summary>
        public void RefreshTagListInListBox()
        {
            try
            {
                Tags.Clear();
                ObservableCollection<Tag> tempTags = WPFApplication.Database.GetTags(SearchBox.Text);
                foreach (Tag tempTag in tempTags)
                {
                    Tags.Add(tempTag);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("From tbSearchBox_TextChanged\n" + ex.Message);
            }
        }
    }
}
